$(document).ready(function(){
    var carpool_starttime=$(".car-pool-city-start-time").children("input[type=text]").val();
    var input_find=$(".car-block:visible").find("input[type=text]");
    var type_x=$(".car-pool-city-nav").val();
	$(".order-submit button").click(function(){
		var data1=$("#d241").val();
		var data2=$("#d242").val();
		oldTime = (new Date(data1)).getTime();
		oldTime1 = (new Date(data2)).getTime();
	});
    
    $(".car-pool-city-nav").click(function(){
        type_x=$(this).val();
        $(".car-pool-city-select").append("<option"+" class='car-pool-city-option'"+">"+"出租车"+"</option>").attr("disabled",true);
        $(".car-pool-city-option").prevAll().remove();
    });
    var car_pool_select=$(".car-block:visible .car-pool-city-select");
    $(".car-pool-sendplane-nav").click(function(){
       type_x=$(this).val();
       $(".car-pool-city-option").remove();
       $(".car-pool-city-select").removeAttr("disabled");
       $(".car-pool-city-select").append("<option"+" class='car-pool-city-option2'"+">"+"普通车"+"</option>");
       $(".car-pool-city-select").append("<option"+">"+"7座商务车"+"</option>");
       $(".car-pool-city-select").append("<option"+">"+"豪华车"+"</option>");
       if ($(".car-pool-sendplane-nav").hasClass("bg")) {
        console.log("yes");
        }
    });

    
    
       

     function input_val(){ 
        var input_age=$(".car-block:visible").find("input[type=text]").length;
        for(var i=0;i<input_age;i++){
            if ($(".car-block:visible").find("input[type=text]").eq(i).val()!="") {
                return true;
            }else{
                return false;
            }
        }
    };
    function car_pool_vehicle_c(value){
        if (value==0) {
            return "普通车";
        }else if (value==1) {
            return "7座商务车";
        }else if (value==2) {
            return "豪华车";
        }else if (value==3) {
            return "出租车";
        }else if (value==4) {
            return "电调车";
        }
    };
    console.log(car_pool_vehicle_c(4));
    function ajax_post(){
        $.ajax({
            type: "POST",
            url: "http://localhost:8085/orders/carpool",
            data: {
                user_id:0,//申请人ID

                origin_name:input_find.eq(0).val(),//出发点名称
                destination_name:input_find.eq(1).val(),//目的地名称
                origin_gps_x:x,//出发点坐标lat
                origin_gps_y:y,//出发点坐标lnt
                destination_gps_x:x1,//目的地坐标lat
                destination_gps_y:y1,//目的地坐标lnt
                vehicle:car_pool_vehicle,//车型（0-普通车，1-商务车，2-豪华车，3-普通出租车，4-电调车）

                departure_time_start:oldTime,//出发时间段-开始
                departure_time_end:oldTime1,//出发时间段-结束
                available_carpool_num:$(".select-number-top").val(),//拼车人数上限
                departure_carpool_num:$(".select-number-bottom").val(),//拼车人数下限
                luggage_limits:$(".luggage-number").val(),//行李最大数量
                gender:sex_1,//性别（0-不限，1-男，2-女）
                allow_free:allow_1,//是否允许化缘（0-否，1-是）
                description:$(".description").val(),//备注
                free_carpool_num:1,//化缘人数
                
                type:type_x,//（1-拼车，2-专车，3-租车，4-慈善，5-许愿，6-送机（不拼车），7-送机（拼车），8-接送机，9-接机）
                carpool_type:0,//拼车类型                
                departure_time:oldTime,
                //is_airport_line:1,
            },
            dataType: "json",
            success: function(data){
				var select_sex ,select_allow;
                var date_1=(data._carpool.departure_time_start-new Date().getTime())/1000-3600*3;
                setInterval(function(){
                    date_1-=1;
                    var h = parseInt(date_1/60/60) ;
                    var m=parseInt(date_1/60-h*60) ;
                    var s = parseInt(date_1-m*60-h*60*60); 
                    if(s<10){
                        $(".p5").html(h+":"+m+":0"+s);
                    }else{
                        $(".p5").html(h+":"+m+":"+s);
                    }
                },1000);
				//判断是否允许化缘
				if(data.allow_free==0){
					select_allow="否";
				}else{
					select_allow="是";
				}
				//判断性别
				if(data._carpool.gender==0){
					select_sex = "男";
				}else if(data._carpool.gender==1){
					select_sex = "女";
				}else{
					select_sex = "未标注性别";
				}
                var order_left_div = $(".order-left-content div");
                $(".order-left-content").append("<div>"+data._carpool.id+"</div>")
                $(".order-number").html("订单编号："+data.sn);
                order_left_div.eq(0).html('发车地点：'+data.origin_name);
                order_left_div.eq(1).html('目的地：'+data.destination_name);
                $(".p3").html(new Date(data._carpool.departure_time_start).toLocaleDateString()+new Date(data._carpool.departure_time_start).toLocaleTimeString());
                $(".p4").html(data.origin_name);
                $(".p6").html(data._creator.nick_name);
                $(".p7").html(data._carpool.description);
                $(".p8").html(car_pool_vehicle_c(car_pool_vehicle));
                $(".p9").html(data.order_price+"元");
                $(".p10").html(data._carpool.available_carpool_num+"人");
                $(".p11").html(data._carpool.departure_carpool_num+"人");
                $(".p12").html(data._carpool.luggage_limits+"件");
                $(".p13").html(select_allow);
                $(".p14").html(select_sex);
            },
            error: function(jqXHR){
                console.log(jqXHR);
            }
        });
    };
    var car_pool_vehicle;
    $(".order-submit button").click(function(){
        if (input_val()) {
            $(".car-block").hide();
            $(".order-block").fadeIn();
            ajax_post();
        }else{
            alert("还有没输入的！")
        }
        if ($(".car-pool-city-select").val()=="普通车") {
            car_pool_vehicle=0;
        };
        if ($(".car-pool-city-select").val()=="豪华车") {
            car_pool_vehicle=2;
        };
        if ($(".car-pool-city-select").val()=="7座商务车") {
            car_pool_vehicle=1;
        };
        if ($(".car-pool-city-select").val()=="出租车") {
            car_pool_vehicle=3;
        };
        
    });
	$("#d242").blur(function(){
    	var data3=$("#d241").val();
    	var oldTime2 = (new Date(data3)).getTime();
    	var data4=$("#d242").val();
    	oldTime3 = (new Date(data4)).getTime();
    	var currentTime2 = new Date().getTime();
    	if((oldTime2-currentTime2)<3600*1000||oldTime3<oldTime2){
    		alert("请提前3小时预订  结束时间必须大于出发时间");
    	};
    });
});